/**
 * Copyright (C) 2011 The Android Open Source Project 
 * Emial : guoyil199@163.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */ 
package com.wifibrowser.lib;

import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;


/**
 * @author Administrator
 * @Email guoyil199@163.com
 * @version V1.0
 * @Title <B>todo</B>
 * <pre>todo</pre>
 */

public abstract class BaseActivity extends ActionBarActivity {
	protected TextView mcTitle;
	
	protected abstract @LayoutRes int onCreateContentView();
	protected abstract void init(Bundle savedInstanceState);
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		onCreateActionBar();
		setContentView(onCreateContentView());
		init(savedInstanceState);
	}

	
	
	public void onCreateActionBar() {
		final ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayShowCustomEnabled(true);
		mcTitle=new TextView(this);
		mcTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
		mcTitle.setTextColor(Color.WHITE);
		mcTitle.setGravity(Gravity.CENTER);
		ActionBar.LayoutParams mCustomViewLayoutParams = new ActionBar.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		mCustomViewLayoutParams.gravity = Gravity.CENTER;
		actionBar.setCustomView(mcTitle, mCustomViewLayoutParams);
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setHomeButtonEnabled(false);
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setDisplayUseLogoEnabled(false);
	}
	
	public void setHomeAsUpIndicator(int desRid){
		getSupportActionBar().setHomeAsUpIndicator(desRid);
	}
	

	/* (non-Javadoc)
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 * <pre>todo</pre>
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId()==android.R.id.home){
			onMenuBackPressed();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void onMenuBackPressed(){
		super.onBackPressed();	
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}
	
	@Override
	public void setTitle(CharSequence title) {
		if (null != mcTitle) {
			mcTitle.setText(title);
			super.setTitle("");
		}else{
			super.setTitle(title);
		}
	}

	@Override
	public void setTitle(int resid) {
		setTitle(getText(resid));
	}
	
	public void setDisplayBackView(boolean isEnabled) {
		getSupportActionBar().setDisplayHomeAsUpEnabled(isEnabled);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		ToastCompat.cancel();
	}
	
	public final void toast(String text){
		ToastCompat.showToast(this, text);
	}
	
	ProgressDialog pDialog;
	public void showProgDialog() {
		if (pDialog == null) {
			pDialog = ProgressDialog.show(this, null, "请稍等...");
		}
		if (!pDialog.isShowing()) {
			pDialog.show();
		}
	}

	public void disProgDialog() {
		if (pDialog != null && pDialog.isShowing()) {
			pDialog.dismiss();
		}
		pDialog = null;
	}
	
}
